package com.authine.cloudpivot.ext.entity.order.extendsAttr;

import cn.hutool.core.lang.Console;
import com.alibaba.cola.exception.BizException;
import com.authine.cloudpivot.ext.enums.order.OrderReturnBehaviorEnum;
import com.authine.cloudpivot.ext.utils.RegexUtils;
import com.authine.cloudpivot.ext.utils.kingdee.DateUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.IOException;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * 退款记录扩展字段
 *
 * @author 张天赐
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class OrderGoodsReturnsExtendsAttr implements Serializable {

    private static final long serialVersionUID = -3159810576922860183L;

    /**
     * 退款记录
     */
    private List<OrderGoodsReturnsLog> logs;

    public static OrderGoodsReturnsExtendsAttr conversion(String value, boolean ifThrow) {
        OrderGoodsReturnsExtendsAttr result = null;
        ObjectMapper objectMapper = DateUtils.objectMapper();

        try {
            if (RegexUtils.notNull(value)) {
                result = objectMapper.readValue(value, OrderGoodsReturnsExtendsAttr.class);
            }
        } catch (IOException ex) {
            Console.error(ex);
            if (ifThrow) {
                throw new BizException("退款商品记录扩展属性字符串转换对象失败");
            }
        }

        return result;
    }

    public static String conversion(OrderGoodsReturnsExtendsAttr value, boolean ifThrow) {
        String result = null;
        ObjectMapper objectMapper = DateUtils.objectMapper();

        try {
            if (null != value) {
                result = objectMapper.writeValueAsString(value);
            }
        } catch (IOException ex) {
            Console.error(ex);
            if (ifThrow) {
                throw new BizException("退款商品记录扩展属性对象转换字符串失败");
            }
        }

        return result;
    }

    /**
     * 退申请日志
     *
     * @param memo           客户申请备注
     * @param operatorUserId 操作人平台用户ID
     * @param operator       操作人角色+名称
     * @return 扩展字段JSON字符串
     */
    public static String applyRefund(String memo, Long operatorUserId, String operator) {
        OrderGoodsReturnsExtendsAttr extendsAttr = OrderGoodsReturnsExtendsAttr.builder()
                .logs(Collections.singletonList(OrderGoodsReturnsLog.build(OrderReturnBehaviorEnum.apply, memo, operatorUserId, "客户-" + operator)))
                .build();
        return OrderGoodsReturnsExtendsAttr.conversion(extendsAttr, true);
    }

    /**
     * 系统审批日志
     *
     * @param behavior       操作行为枚举（ApproveReject、ApproveAgree）
     * @param extendsAttrStr 已存在的扩展字段（必填）
     * @param memo           系统审批后的备注
     * @return 扩展字段JSON字符串
     */
    public static String approve(OrderReturnBehaviorEnum behavior, String extendsAttrStr, String memo) {
        OrderGoodsReturnsExtendsAttr extendsAttrObj = OrderGoodsReturnsExtendsAttr.conversion(extendsAttrStr, true);
        List<OrderGoodsReturnsLog> logs = extendsAttrObj.getLogs();
        logs.add(OrderGoodsReturnsLog.build(behavior, memo, 1L, "平台-系统审批"));
        return OrderGoodsReturnsExtendsAttr.conversion(extendsAttrObj.setLogs(logs), true);
    }

    /**
     * 发退货日志
     *
     * @param extendsAttrStr 已存在的扩展字段（必填）
     * @param operatorUserId 操作人平台用户ID
     * @param operator       操作人角色+名称
     * @return 扩展字段JSON字符串
     */
    public static String shipmentReturn(String extendsAttrStr, Long operatorUserId, String operator) {
        OrderGoodsReturnsExtendsAttr extendsAttrObj = OrderGoodsReturnsExtendsAttr.conversion(extendsAttrStr, true);
        List<OrderGoodsReturnsLog> logs = extendsAttrObj.getLogs();
        logs.add(OrderGoodsReturnsLog.build(OrderReturnBehaviorEnum.DoneDelivery, "", operatorUserId, operator));
        return OrderGoodsReturnsExtendsAttr.conversion(extendsAttrObj.setLogs(logs), true);
    }

    /**
     * 收退货日志
     *
     * @param extendsAttrStr 已存在的扩展字段（必填）
     * @param operatorUserId 操作人平台用户ID
     * @param operator       操作人角色+名称
     * @return 扩展字段JSON字符串
     */
    public static String receiveReturns(String extendsAttrStr, Long operatorUserId, String operator) {
        OrderGoodsReturnsExtendsAttr extendsAttrObj = OrderGoodsReturnsExtendsAttr.conversion(extendsAttrStr, true);
        List<OrderGoodsReturnsLog> logs = extendsAttrObj.getLogs();
        logs.add(OrderGoodsReturnsLog.build(OrderReturnBehaviorEnum.Done, "", operatorUserId, operator));
        return OrderGoodsReturnsExtendsAttr.conversion(extendsAttrObj.setLogs(logs), true);
    }
}
